package pl.wr.physics.units.quantum.number;

import pl.wr.physics.exceptions.QuantumException;
import pl.wr.physics.units.AbstractUnit;
import pl.wr.physics.units.SI.Prefix;
import pl.wr.physics.units.quantum.QuantumUnit;

/**
 * @author wieslaw.rodak
 * 
 */
public class MagneticQuantumNumber implements QuantumUnit {

    /**
     * Magnetic quantum number.
     */
    private int ml;

    /**
     * Make magnetic quantum number from range: -6 to 6. Oversize throw
     * QuantumException.
     * 
     * @param ml
     *            - int value of magnetic quantum number
     */
    public MagneticQuantumNumber(int ml) {
        checkScope(ml);
        this.ml = ml;
    }

    /**
     * @return int value of magnetic quantum number.
     */
    public int getMl() {
        return ml;
    }

    /**
     * Set the magnetic quantum number from range: -6 to 6. Oversize throw
     * QuantumException.
     * 
     * @param ml
     *            - int value of magnetic quantum number.
     */
    public void setMl(int ml) {
        checkScope(ml);
        this.ml = ml;
    }

    private static void checkScope(int ml) {
        if (ml >= -6 && ml <= 6) {
            // is OK
        } else {
            throw new QuantumException("Bad scope of number: " + ml);
        }
    }

    private static class MagneticQuantumNumberCache {
        static final MagneticQuantumNumber cache[] = new MagneticQuantumNumber[13];

        static {
            int j = -6;
            for (int k = 0; k < cache.length; k++)
                cache[k] = new MagneticQuantumNumber(j++);
        }

        private MagneticQuantumNumberCache() {
        }
    }

    /**
     * Returns an MagneticQuantumNumber instance representing the specified int
     * value. This method will always cache values in the range from -6 to 6.
     * 
     * @param l
     * @return
     */
    public static MagneticQuantumNumber valueOf(int l) {
        checkScope(l);
        return MagneticQuantumNumberCache.cache[l + 6];
    }

    @Override
    public AbstractUnit getSIValue() {
        return new AbstractUnit(Prefix.basic, ml, null) {
        };
    }
}
